<p>
  A common problem in linear algebra is solving linear equations. Consider the following linear equations:
</p>

\[  2x + y - z = 8    \]
\[ -3x - y + 2z = -11 \]
\[ -2x + y + 2z = -3  \]

<p>
  If we let:
</p>
\[ A = \begin{pmatrix}
   2  &amp; 1  &amp; -1 \\
   -3 &amp; -1 &amp; 2  \\
   -2 &amp; 1  &amp; 2
\end{pmatrix} \qquad
\mathbf{x} = \begin{pmatrix} x \\  y  \\ z  \end{pmatrix} \qquad
\mathbf{b} = \begin{pmatrix} 8 \\ -11 \\ -3 \end{pmatrix} \]

<p>
  Then the linear equations above can be written as \( A\mathbf{x} = \mathbf{b} \)
</p>

<p>
  If A is invertible, then we can multiply \(A^{-1}\) on both sides of the equation to obtain the solution:
</p>
\[ A^{-1}A \mathbf{x} = A^{-1}\mathbf{b} \]
\[ \mathbf{x} = A^{-1}\mathbf{b} \]
<p>
  As long as \(A^{-1}\) exists, we can compute it to solve the linear equations:
</p>

<div class="section-example-container">

<pre class="python">A = np.array([[2,1,-1],[-3,-1,2],[-2,1,2]])
b = np.array([[8],[-11],[-3]])
inv_A = np.linalg.inv(A)
print np.dot(inv_A, b)
[out]:
[[ 2.]
 [ 3.]
 [-1.]]
</pre>
</div>

<p>
  The solution is <em>x</em> = 2, <em>y</em> = 3, <em>z</em> = &minus;1. However, computing the inverse matrix is not recommended, since it is numerically unstable i.e. small rounding errors can dramatically affect the result.
</p>

<p>
  Instead, NumPy solves linear equations by <a href="https://www.youtube.com/watch?v=m3EojSAgIao" target="_blank">LU decomposition</a>:
</p>

<div class="section-example-container">

<pre class="python">print np.linalg.solve(A, b)
[out]:
[[ 2.]
 [ 3.]
 [-1.]]
</pre>
</div>

<p>
  Of course, we get the same solution. We can check the correctness of the solution by substituting <em>x</em>, <em>y</em> and <em>z</em> into the linear equations.
</p>
